class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size()==0){
            return 0;
        }
        /*
        int min=prices[0];
        int profile=0;
        int sum=0;
        prices.push_back(0);
        for(int i=1;i<prices.size();i++){
            if(prices[i]<prices[i-1]){
                min=prices[i];
                sum=sum+profile;
                profile=0;
                //cout<<sum<<endl;
            } else{
                profile=profile>prices[i]-min?profile:prices[i]-min;
                //cout<<"  "<<profile<<endl;
            }
        }
        return sum;
        */
        int min=prices[0];
        int profile=0;
        for(int i=1;i<prices.size();i++){
            if(prices[i]>prices[i-1]){
                profile+=prices[i]-prices[i-1];
            }
        }
        return profile;
    }
};

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        /*
        vector<vector<int>> dp(prices.size()+1,vector<int>(2,0));
        dp[0][1]=INT_MIN;
        for(int i=0;i<prices.size();i++){
            dp[i+1][0]=max(dp[i][0], dp[i][1]+prices[i]);
            dp[i+1][1]=max(dp[i][1], dp[i][0]-prices[i]);
        }
        return dp[prices.size()][0];
        */

        int dp_i_0=0;
        int dp_i_1=INT_MIN;
        for(int i=0;i<prices.size();i++){
            dp_i_0=max(dp_i_0,dp_i_1+prices[i]);
            dp_i_1=max(dp_i_1,dp_i_0-prices[i]);
        }
        return dp_i_0;
    }   
};

//dp[i][k][0/1] i:第i天，k交易次数，本例中为无穷大，0手上没有股票，1,手上有股票
//dp[i+1][k][0]=max(dp[i][k][0],dp[i][k][1]+prices[i]) 
//dp[i+1][k][1]=max(dp[i][k][1],dp[i][k-1][0]-prices[i])